//
//  LoginViewController.m
//  duodiandaohang
//
//  Created by jzb on 2018/2/7.
//  Copyright © 2018年 jzb. All rights reserved.
//

#import "LoginViewController.h"
#import "BaseNavigationC.h"
//#import "HomeViewController.h"
#import "RegViewController.h"
#import "CodeLoginVC.h"
#import "ForgetPWVC.h"


@interface LoginViewController ()<UITextFieldDelegate>
{
    NSString *sid;
}
@property (weak, nonatomic) IBOutlet UIImageView *LOGOi;
@property (weak, nonatomic) IBOutlet UITextField *phoneField;
@property (weak, nonatomic) IBOutlet UITextField *passwordFiled;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property (weak, nonatomic) IBOutlet UIButton *regesterBtn;
@property (weak, nonatomic) IBOutlet UIButton *forgetBtn;
@property (weak, nonatomic) IBOutlet UIView *colorBackV;


@end

@implementation LoginViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    self.navigationController.navigationBarHidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
//    self.navigationController.navigationBarHidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"登录";
    //    self.colorBackV.backgroundColor = [UIColorHex(0xB58522) colorWithAlphaComponent:0.7];
    self.colorBackV.layer.cornerRadius = 6;
    self.colorBackV.layer.masksToBounds = YES;
    
    if (@available(iOS 10.0, *)) {
        self.RegBut.layer.borderColor = [UIColor colorWithDisplayP3Red:238/255.0 green:167/255.0 blue:43/255.0 alpha:0.5].CGColor;
        
    } else {
        
    }
    self.RegBut.layer.borderWidth = 1;
//    [_phoneField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
//    [_phoneField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
//    [_passwordFiled setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
//    [_passwordFiled setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
    self.LOGOi.layer.cornerRadius = 10;
    self.LOGOi.layer.masksToBounds = YES;
//    _phoneField.delegate = self;
    
   
    self.phoneField.text = k_user_phone;
    
    self.passwordFiled.text = k_user_pwd;

}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
        if ([textField isEqual:self.phoneField]) {
            NSUInteger length = textField.text.length + string.length - range.length;
            if (length <= 11) {
                return YES;
            }else{
                [self.phoneField shakeWithShakeDirection:ShakingDirectionHorizontal];
                return NO;
            }
        }else if ([textField isEqual:self.passwordFiled]) {
            NSUInteger length = textField.text.length + string.length - range.length;
            if (length <= 6) {
                return YES;
            }else{
                [self.passwordFiled shakeWithShakeDirection:ShakingDirectionHorizontal];
                return NO;
            }
        }else{
            return YES;
        }
}

#pragma mark -- 登录
- (IBAction)loginBtnClick:(UIButton *)sender {
   
//    UIWindow *win = [UIApplication sharedApplication].keyWindow;
//    HomeViewController *home = [[HomeViewController alloc] init];
//    BaseNavigationC *navi = [[BaseNavigationC alloc] initWithRootViewController:home];
//    navi.navigationBar.barTintColor = [UIColor colorWithHexString:@"58D27C"];// 设置导航栏背景颜色
//    //    navi.navigationBar.barTintColor = [UIColor blackColor];// 设置导航栏背景颜色
//    
//    [navi.navigationBar setTitleTextAttributes:
//     @{NSFontAttributeName:[UIFont systemFontOfSize:18],
//       NSForegroundColorAttributeName:[UIColor whiteColor]}];// 设置导航栏文字字体大小 文字的颜色
//    
//    win.rootViewController = navi;
//
    
    //登录
    if (kStringIsEmpty(self.phoneField.text)) {
        [self showBottomMessag:@"请输入手机号"];
        return;
    }else if (![NSString IsPhoneNumber:self.phoneField.text]) {
        [self showBottomMessag:@"请输入正确的手机号"];
        return;
    }else if (self.passwordFiled.text.length < 6) {
        [self showCenterMessage:@"密码长度最少六位"];
        return;
    }

    
    if (self.phoneField.text.length == 11 && self.passwordFiled.text.length > 0) {
        [[DDNetworkManagerDate makeUrl_login_with_user_phone:self.phoneField.text user_password:self.passwordFiled.text] post_RequestFinshSuccess:^(id responseObject) {
            
            NSLog(@"-=-=-=-=>>>%@",responseObject);
            if ([self requestFinshCode:responseObject isShowMessage:YES] == 1) {
                //登录环信
                 __weak typeof(self) weakSelf = self;
                [[EMClient sharedClient] loginWithUsername:self.phoneField.text
                                                  password:kEMPassword
                                                completion:^(NSString *aUsername, EMError *aError) {
                                                    if (!aError) {
                                                        NSLog(@"登录成功");
                
                                                        [kUserDefaults setObject:self.phoneField.text forKey:@"user_phone"];
                                                        [kUserDefaults setObject:self.passwordFiled.text forKey:@"user_pwd"];
                                                        NSDictionary *data = [responseObject objectForKey:kRequsetDataKey];
                                                        [kUserDefaults setObject:data[@"user_token"] forKey:@"user_token"];
                                                        [self net_getUserInfo];
                                                        NSNotification *notification =[NSNotification notificationWithName:@"k_notify_updateMeVC" object:nil userInfo:nil];
                                                        [[NSNotificationCenter defaultCenter] postNotification:notification];
//
                                                        [weakSelf goBack];
                                                        
                                                    } else {
                                                        NSLog(@"登录失败");
                                                    }
                                                }];

            }
        } failure:^(id errorObject) {
        }];
    }else{
        [self showCenterMessage:@"请正确输入"];
    }
    
}
//获取用户信息
-(void)net_getUserInfo{
    [[DDNetworkManagerDate makeUrl_getuserinfo_with_token:k_user_token] post_RequestFinshSuccess:^(id responseObject) {
        if ([self requestFinshCode:responseObject isShowMessage:NO] == 1) {
            NSDictionary *data = [responseObject objectForKey:kRequsetDataKey];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"user_id"]] forKey:@"user_id"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"user_nickname"]] forKey:@"user_nickname"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"user_portrait"]] forKey:@"user_headsmall"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"user_money"]] forKey:@"user_money"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"user_truename"]] forKey:@"user_truename"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"user_birthday"]] forKey:@"user_birthday"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"user_sex"]] forKey:@"user_sex"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"media_id"]] forKey:@"media_id"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"shop_id"]] forKey:@"shop_id"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"user_sign"]] forKey:@"user_sign"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"user_qrcode"]] forKey:@"user_qrcode"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"user_sn"]] forKey:@"user_sn"];

            [kUserDefaults synchronize];
        }
    } failure:^(id errorObject) {
        [self showBottomMessag:@"网络请求失败"];
    }];
}
- (IBAction)RegButOnclick:(UIButton *)sender {
    
    RegViewController *RegVC = [[RegViewController alloc]init];
    
    [self.navigationController pushViewController:RegVC animated:YES];
}
- (IBAction)CodeLoginBtnOnclick:(UIButton *)sender {
    
    CodeLoginVC *CodeVC = [[CodeLoginVC alloc]init];
    
    [self.navigationController pushViewController:CodeVC animated:YES];
}
- (IBAction)NoRegBtnOnclick:(UIButton *)sender {
    
    [self goBack];
}
- (IBAction)ForgetPasBtnOnclick:(UIButton *)sender {
    
    
    ForgetPWVC *VC = [[ForgetPWVC alloc]init];
    
    [self.navigationController pushViewController:VC animated:YES];
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
